package com.jizi.machine.controller;

import com.alibaba.fastjson.JSONObject;
import com.jizi.machine.service.TencentOCRService;
import com.jizi.machine.vo.OcrBankCardParam;
import com.jizi.machine.vo.OcrIdCardParam;
import com.jizi.machine.vo.OcrLicenseParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * ocr识别接口
 *
 * @author laijiangfeng
 * @date 2025/2/13 16:24
 */
@RestController
@RequestMapping("/p/ocr")
@AllArgsConstructor
public class OCRController {

    @Resource
    private TencentOCRService ocrService;

    /**
     * 身份证识别接口
     *
     * @param ocrIdCardParam
     * @return
     */
    @PostMapping("/idcard")
    public String recognizeIDCard(@RequestBody OcrIdCardParam ocrIdCardParam) {
        try {
            String result = ocrService.recognizeIDCardByImageBase64(ocrIdCardParam.getImageBase64(), ocrIdCardParam.getImageUrl(), ocrIdCardParam.getCardSide());
            JSONObject cardInfo = JSONObject.parseObject(result);
            return cardInfo.toJSONString();
        } catch (Exception e) {
            e.printStackTrace();
            return "识别失败: " + e.getMessage();
        }
    }

    /**
     * 银行卡识别接口
     *
     * @param ocrBankCardParam
     * @return
     */
    @PostMapping("/bankcard")
    public String recognizeBankCard(@RequestBody OcrBankCardParam ocrBankCardParam) {
        try {
            String result = ocrService.recognizeBankCardByImageBase64(ocrBankCardParam.getImageBase64(), ocrBankCardParam.getImageUrl());
            JSONObject cardInfo = JSONObject.parseObject(result);
            return cardInfo.toJSONString();
        } catch (Exception e) {
            e.printStackTrace();
            return "识别失败: " + e.getMessage();
        }
    }

    /**
     * 企业营业执照识别接口
     * @param ocrLicenseParam
     * @return
     */
    @PostMapping("/businessLicense")
    public String recognizeBusinessLicense(@RequestBody OcrLicenseParam ocrLicenseParam) {
        try {
            String result = ocrService.recognizeBusinessLicenseByImageBase64(ocrLicenseParam.getImageBase64(), ocrLicenseParam.getImageUrl());
            JSONObject cardInfo = JSONObject.parseObject(result);
            return cardInfo.toJSONString();
        } catch (Exception e) {
            e.printStackTrace();
            return "识别失败: " + e.getMessage();
        }
    }
}